Make the jobs index test more representative

Fill in the handler YAML for the test jobs instead of using only dummy empty
jobs. 2 of the jobs are also customized -- one has been locked and the other
is a job scheduled, but then it's associated agent had been deleted.

Incorporates @dsander's review comments.

Colin Shea 10 年 前
コミット
1d3535d65a
共有2 個のファイルを変更した13 個の追加6 個の削除を含む
  1. 3 3
      app/views/jobs/index.html.erb
  2. 10 3
      spec/controllers/jobs_controller_spec.rb

+ 3 - 3
app/views/jobs/index.html.erb

@@ -20,11 +20,11 @@
20 20
           </tr>
21 21
 
22 22
         <% @jobs.each do |job| %>
23
-          <% agent = Agent.find(YAML.load(job.handler).args[0]) %>
23
+          <% agent = Agent.find_by_id(YAML.load(job.handler).args[0]) %>
24 24
           <tr>
25 25
             <td><%= status(job) %></td>
26
-            <td><%= link_to agent.name, agent_path(agent) %></td>
27
-            <td title='<%= job.created_at %>'><%= time_ago_in_words job.created_at %> ago for <%= agent.user.username %></td>
26
+            <td><%= agent ? link_to(agent.name, agent_path(agent)) : "(deleted)" %></td>
27
+            <td title='<%= job.created_at %>'><%= time_ago_in_words job.created_at %> ago <%= agent ? "for #{agent.user.username}" : '' %></td>
28 28
             <td title='<%= job.run_at %>'>
29 29
               <% if !job.failed_at %>
30 30
                 <%= relative_distance_of_time_in_words job.run_at %>

+ 10 - 3
spec/controllers/jobs_controller_spec.rb

@@ -4,8 +4,15 @@ describe JobsController do
4 4
 
5 5
   describe "GET index" do
6 6
     before do
7
-      Delayed::Job.create!
8
-      Delayed::Job.create!
7
+      async_handler_yaml =
8
+        "--- !ruby/object:Delayed::PerformableMethod\nobject: !ruby/class 'Agent'\nmethod_name: :async_check_without_delay\nargs:\n- %d\n"
9
+
10
+      Delayed::Job.create!(handler: async_handler_yaml % [agents(:jane_website_agent).id])
11
+      Delayed::Job.create!(handler: async_handler_yaml % [agents(:bob_website_agent).id])
12
+      Delayed::Job.create!(handler: async_handler_yaml % [agents(:jane_weather_agent).id])
13
+      agents(:jane_website_agent).destroy
14
+      Delayed::Job.create!(handler: async_handler_yaml % [agents(:bob_weather_agent).id], locked_at: Time.now, locked_by: 'test')
15
+
9 16
       expect(Delayed::Job.count).to be > 0
10 17
     end
11 18
 
@@ -19,7 +26,7 @@ describe JobsController do
19 26
       expect(users(:jane)).to be_admin
20 27
       sign_in users(:jane)
21 28
       get :index
22
-      expect(assigns(:jobs).length).to eq(2)
29
+      expect(assigns(:jobs).length).to eq(4)
23 30
     end
24 31
   end
25 32